package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    public static final Integer DEFAULT_YES = 1;

    /**
     * 查询所有地址信息
     */
    @Override
    public List<AddressBook> list() {
        AddressBook addressBook=AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return addressBookMapper.selectAll(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook select() {
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBookNew =AddressBook.builder()
                .userId(userId)
                .isDefault(DEFAULT_YES)
                .build();
        AddressBook addressBook= addressBookMapper.select(addressBookNew);
        return addressBook;
    }

    /**
     * 添加地址
     * @param addressBook
     */
    @Override
    public void insert(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {


//        //设置默认地址先查询之前的是否有,创建一个地址对象封装一个假定默认地址查询
//        AddressBook addressBookOld=AddressBook.builder()
//                .userId(BaseContext.getCurrentId())
//                .isDefault(StatusConstant.ENABLE)
//                .build();
//        AddressBook OldAddressBook = addressBookMapper.select(addressBookOld);
//        //如果假定默认地址返回的结果不是空，并且不是这个地址本身就把旧的默认地址改掉。如果是空就直接设置
//        if (OldAddressBook != null && OldAddressBook.getId()!=addressBook.getId()) {
//            //取消原来的默认地址
//            addressBookOld.setIsDefault(StatusConstant.DISABLE);
//            addressBookMapper.update(addressBookOld);
//
//            //设置新的默认地址
//            addressBook.setIsDefault(StatusConstant.ENABLE);
//        }else if (OldAddressBook != null && OldAddressBook.getId()==addressBook.getId()){
//            //如果要设置的本身就是默认就取消默认
//            addressBook.setIsDefault(StatusConstant.DISABLE);
//        }else if (OldAddressBook == null){
//            //如果没有默认地址就直接把这个地址设为默认
//            addressBook.setIsDefault(StatusConstant.ENABLE);
//        }

        //如果要修改默认地址原本就是默认地址就修改全部地址为非默认，如果不是就修改全部地址为非默认，再把这个地址设置成默认

        Integer isDefault = addressBookMapper.select(addressBook).getIsDefault();

        //把用户id设置进去
        addressBook.setUserId(BaseContext.getCurrentId());

        if (isDefault == StatusConstant.DISABLE) {
            //要修改默认地址本身不是默认地址
            addressBook.setIsDefault(StatusConstant.ENABLE);
        }else {
            //要修改默认地址本身是默认地址
            addressBook.setIsDefault(StatusConstant.DISABLE);
        }
        //修改当前用户所有地址为非默认
        addressBookMapper.updateDisable(addressBook);

        //修改当前用户默认地址
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook selectById(Long id) {

        return addressBookMapper.selectById(id);
    }

    /**
     * 修改地址
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    /**
     * 删除地址
     * @param id
     */
    @Override
    public void delete(Long id) {
        AddressBook addressBook=AddressBook.builder()
                .id(id)
                .build();
        addressBookMapper.delete(addressBook);
    }
}
